﻿
Public MustInherit Class basicJob(Of impl_t As ijob, hash_t)
    Implements ijob

    Protected Overridable Sub auto_fill()
    End Sub

    Protected Overridable Sub receive_lop()
    End Sub

    Public Sub ijob_receive_lop() Implements ijob.receive_lop
        auto_fill()
        receive_lop()
    End Sub

    Protected Overridable Sub respond_lop()
    End Sub

    Public Sub ijob_respond_lop() Implements ijob.respond_lop
        respond_lop()
    End Sub

    Protected Overridable Function job_valid() As Boolean
        Return True
    End Function

    Public Function valid() As Boolean Implements ijob.valid
        Return job_valid()
    End Function

    Protected Shared Sub copy(Of T)(ByRef o As T, ByVal i As T)
        Global.copy(o, i)
    End Sub

    Protected MustOverride Sub copy(ByVal i As impl_t)
    Public Function copy(ByVal i As ijob) As Boolean Implements ijob.copy
        Dim imp As impl_t = Nothing
        If convertor.cast(Of impl_t)(i, imp) Then
            Dim cmp As SByte = 0
            cmp = object_compare(Me, imp)
            assert(cmp = 0 OrElse cmp = object_compare_undetermined)
            If cmp = object_compare_undetermined Then
                copy(imp)
            End If
            Return True
        Else
            Return False
        End If
    End Function

    Public Function copy(ByVal i As indexedJob) As Boolean Implements ijob.copy
        If i Is Nothing Then
            Return False
        Else
            Return copy(i.job())
        End If
    End Function

    Protected MustOverride Function hashcode_key() As hash_t
    Public NotOverridable Overrides Function GetHashCode() As Int32
        Dim i As hash_t = Nothing
        i = hashcode_key()
        If i Is Nothing Then
            Return 0
        Else
            Return i.GetHashCode()
        End If
    End Function

    Protected MustOverride Function types() As Type()

    Private _index As Int64 = 0
    Private _paras() As fromBytes
    Protected Function next_para() As fromBytes
        assert(Not _paras Is Nothing)
        assert(_index < _paras.Length())
        Return _paras(inc_(_index))
    End Function

    Protected Overridable Sub frombytes()
        assert(False)
    End Sub

    Protected Overridable Sub frombytes(ByVal paras() As fromBytes)
        _paras = paras
        _index = 0
        frombytes()
    End Sub

    Public Sub frombytes(ByVal b As bytes) Implements frombytes.frombytes
        Dim ts() As Type = Nothing
        ts = types()
        assert(Not ts Is Nothing AndAlso ts.Length() > 0)
        Dim paras() As fromBytes = Nothing
        If deserialize(b, ts, paras) Then
            assert(Not paras Is Nothing AndAlso paras.Length() = ts.Length())
            frombytes(paras)
        End If
    End Sub

    Protected MustOverride Function paras() As toBytes()
    Public Function tobytes() As bytes Implements toBytes.tobytes
        Dim rtn As bytes = Nothing
        If serialize(rtn, False, paras()) Then
            Return rtn
        Else
            Return Nothing
        End If
    End Function

    Protected Overridable Function alloc() As impl_t
        Return Global.alloc(Of impl_t)()
    End Function

    Public Function Clone() As Object Implements ICloneable.Clone
        Dim rtn As impl_t = Nothing
        rtn = alloc()
        assert(rtn.copy(Me))
        Return rtn
    End Function
End Class
